import { ref } from 'vue';
import type { RouteRecordRaw } from 'vue-router';

function loadLocalRoutes() {
	const localRoutes: RouteRecordRaw[] = [];
	// 读取router/main所有的ts文件
	const files: Record<string, any> = import.meta.glob('../router/main/**/*.ts', {
		eager: true,
	});

	// 将加载的对象放到localroutes里面
	for (const key in files) {
		const module = files[key];
		localRoutes.push(module.default);
	}
	return localRoutes;
}

export function dealArray(data: any[]) {
	function recurseGetPath(menus: any[]) {
		for (const item of menus) {
			if (item.childList && item.childList.length > 0) {
				recurseGetPath(item.childList);
			} else {
				item.path = '/views/' + item.url.slice(0, item.url.length - 11);
			}
		}
	}

	recurseGetPath(data);

	return data;
}

export let firstMenu: any = null;

export function mapMenusToRoutes(userMenus: any[]) {
	// 加载本地路由
	const localRoutes = loadLocalRoutes();

	// 根据菜单匹配正确的路由
	const routes: RouteRecordRaw[] = [];

	for (const menu of userMenus) {
		for (const submenu of menu.childList) {
			const route = localRoutes.find((item) => item.name === submenu.name);
			if (route) {
				// 给顶层的菜单增加重定向功能（但是只添加一次）
				if (!routes.find((item) => item.name === menu.name)) {
					routes.push({ path: menu.url, redirect: route.path });
				}
				// 将二级菜单添加到路由里面
				routes.push(route);
			}
			// 记录第一个匹配到的路由
			if (firstMenu === null && route) firstMenu = submenu;
		}
	}
	return routes;
}

export function mapPathToMenu(path: string, userMenus: any[]) {
	for (const menu of userMenus) {
		if (menu.childList.length > 0) {
			for (const submenu of menu.childList) {
				if (submenu.path === path) {
					return submenu;
				}
			}
		} else {
			if (menu.path === path) return menu;
		}
	}
}

interface IBreadcrumbs {
	name: string;
	path: string;
}

export function mapPathToBreadCrumbs(path: string, userMenus: any[]) {
	const breadCrumbs: IBreadcrumbs[] = [];
	for (const menu of userMenus) {
		for (const submenu of menu.childList) {
			if (submenu.path === path) {
				breadCrumbs.push({ name: menu.name, path: menu.path });
				breadCrumbs.push({ name: submenu.name, path: submenu.path });
			}
		}
	}
	return breadCrumbs;
}

// 菜单映射到ID的列表
export function mapMenusListToIds(menuList: any[]) {
	const ids: number[] = [];

	function recurseGetId(menus: any[]) {
		for (const item of menus) {
			if (item.childList) {
				recurseGetId(item.childList);
			} else {
				ids.push(item.id);
			}
		}
	}

	recurseGetId(menuList);

	return ids;
}

// 从菜单映射到按钮权限
export function mapMenusToPermissions(menuList: any[]) {
	const permissions: string[] = [];

	function recurseGetPermission(menus: any[]) {
		for (const item of menus) {
			if (item.type === 3) {
				permissions.push(item.permission);
			} else {
				recurseGetPermission(item.childList ?? []);
			}
		}
	}

	recurseGetPermission(menuList);

	return permissions;
}

// 菜单映射到ID的列表
export function mapChildListToChildren(menuList: any[]) {
	function recurseGetChildren(menus: any[]) {
		for (const item of menus) {
			if (item.childList) {
				item.children = item.childList;
				recurseGetChildren(item.childList);
			}
		}
	}

	recurseGetChildren(menuList);

	return menuList;
}
